using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ICP_MS.Plugin.Tune.Properties;

namespace ICP_MS.Plugin.Tune;

public class TurnCntl : UserControl
{
	public bool bSendValue = false;

	private decimal _increment = 1m;

	private decimal _minValue = default(decimal);

	private double _tipMin = double.NaN;

	private double _tipMax = double.NaN;

	private double _coe = 1.0;

	private decimal _maxValue = default(decimal);

	private decimal _value = default(decimal);

	private int _feedDecimal = 1;

	private IContainer components = null;

	private Label lblText;

	private PictureBox ledFeed;

	private SliderControl sliderControlMs;

	private TextBox txtFeedBack;

	[Description("显示小数点位数，仅仅为显示")]
	[Category("Parameters")]
	public int DecimalPlaces { get; set; }

	[Description("单步值，有效小数位数需小于控件小数点位数")]
	[Category("Parameters")]
	public decimal Increment
	{
		get
		{
			return _increment;
		}
		set
		{
			_increment = value;
			sliderControlMs.MinStep = (double)value;
		}
	}

	[Description("最小值")]
	[Category("Parameters")]
	public decimal MinValue
	{
		get
		{
			return _minValue;
		}
		set
		{
			_minValue = value;
			sliderControlMs.Min = (double)_minValue;
		}
	}

	public double TipMin
	{
		get
		{
			return _tipMin;
		}
		set
		{
			_tipMin = value;
			sliderControlMs.TipMin = _tipMin;
		}
	}

	public double TipMax
	{
		get
		{
			return _tipMax;
		}
		set
		{
			_tipMax = value;
			sliderControlMs.TipMax = _tipMax;
		}
	}

	[Description("系数")]
	[Category("Parameters")]
	public decimal Coe
	{
		get
		{
			return (decimal)_coe;
		}
		set
		{
			_coe = (double)value;
		}
	}

	[Browsable(true)]
	[Category("Parameters")]
	[DisplayName("是否负值")]
	[ReadOnly(false)]
	[DefaultValue(false)]
	public bool Negative
	{
		get
		{
			return _coe < 0.0;
		}
		set
		{
			_coe = ((!value) ? 1 : (-1));
		}
	}

	[Description("最大值")]
	[Category("Parameters")]
	public decimal MaxValue
	{
		get
		{
			return _maxValue;
		}
		set
		{
			_maxValue = value;
			sliderControlMs.Max = (double)_maxValue;
		}
	}

	[Description("当前值")]
	[Category("Parameters")]
	public decimal Value
	{
		get
		{
			return _value * (decimal)_coe;
		}
		set
		{
			_value = value / (decimal)_coe;
			sliderControlMs.Value = (double)_value;
		}
	}

	[Description("控件显示名")]
	[Category("Parameters")]
	[Browsable(true)]
	public string Label
	{
		get
		{
			return lblText.Text;
		}
		set
		{
			lblText.Text = value;
		}
	}

	[Description("线性转换为十六进制的斜率")]
	[Category("Parameters")]
	public double K { get; set; }

	[Description("线性转换为十六进制的截距")]
	[Category("Parameters")]
	public double C { get; set; }

	[Description("CAN目标ID")]
	[Category("Parameters")]
	public uint DestId { get; set; }

	[Description("代码ID，标识自身")]
	[Category("Parameters")]
	public uint CodeId { get; set; }

	[Category("Parameters")]
	[Description("返回值小数位数")]
	public int FeedDecimal
	{
		get
		{
			return _feedDecimal;
		}
		set
		{
			if (value < 1)
			{
				_feedDecimal = 1;
			}
			else if (value > 5)
			{
				_feedDecimal = 5;
			}
			else
			{
				_feedDecimal = value;
			}
		}
	}

	[Browsable(false)]
	public double Defalut { get; set; }

	public int ImageFeed
	{
		set
		{
			Image image = null;
			switch (value)
			{
			case 0:
				image = Resources.led_Gray;
				break;
			case 1:
				image = Resources.led_Green;
				break;
			case 2:
				image = Resources.led_Yello;
				break;
			case 3:
				image = Resources.led_Red;
				break;
			}
			ledFeed.Image = image;
		}
	}

	public event EventHandler ValueChanged;

	public TurnCntl()
	{
		DecimalPlaces = 0;
		InitializeComponent();
		ApplyResources();
	}

	private void ApplyResources()
	{
	}

	private void btnSend_Click(object sender, EventArgs e)
	{
		Task.Factory.StartNew(delegate
		{
			Invoke((EventHandler)delegate
			{
				ledFeed.Image = Resources.led_Green;
			});
			Thread.Sleep(500);
			Invoke((EventHandler)delegate
			{
				ledFeed.Image = Resources.led_Gray;
			});
		});
		_value = (decimal)sliderControlMs.Value;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, null);
		}
	}

	private void sliderControlMs_ValueChanged(object sender, EventArgs e)
	{
	}

	private void ledFeed_Click(object sender, EventArgs e)
	{
		btnSend_Click(null, null);
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.lblText = new System.Windows.Forms.Label();
		this.ledFeed = new System.Windows.Forms.PictureBox();
		this.txtFeedBack = new System.Windows.Forms.TextBox();
		this.sliderControlMs = new ICP_MS.Plugin.Tune.SliderControl();
		((System.ComponentModel.ISupportInitialize)this.ledFeed).BeginInit();
		base.SuspendLayout();
		this.lblText.Font = new System.Drawing.Font("微软雅黑", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		this.lblText.Location = new System.Drawing.Point(4, 3);
		this.lblText.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.lblText.Name = "lblText";
		this.lblText.Size = new System.Drawing.Size(99, 35);
		this.lblText.TabIndex = 2;
		this.lblText.Text = "label";
		this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.ledFeed.Image = ICP_MS.Plugin.Tune.Properties.Resources.led_Gray;
		this.ledFeed.Location = new System.Drawing.Point(241, 8);
		this.ledFeed.Margin = new System.Windows.Forms.Padding(4);
		this.ledFeed.Name = "ledFeed";
		this.ledFeed.Size = new System.Drawing.Size(27, 25);
		this.ledFeed.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
		this.ledFeed.TabIndex = 5;
		this.ledFeed.TabStop = false;
		this.ledFeed.Click += new System.EventHandler(ledFeed_Click);
		this.txtFeedBack.ForeColor = System.Drawing.Color.Red;
		this.txtFeedBack.Location = new System.Drawing.Point(116, 9);
		this.txtFeedBack.Margin = new System.Windows.Forms.Padding(4);
		this.txtFeedBack.Name = "txtFeedBack";
		this.txtFeedBack.ReadOnly = true;
		this.txtFeedBack.Size = new System.Drawing.Size(112, 25);
		this.txtFeedBack.TabIndex = 3;
		this.txtFeedBack.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.txtFeedBack.Visible = false;
		this.sliderControlMs.Coe = 1.0;
		this.sliderControlMs.Location = new System.Drawing.Point(3, 40);
		this.sliderControlMs.Margin = new System.Windows.Forms.Padding(5);
		this.sliderControlMs.Max = 200.0;
		this.sliderControlMs.Min = 1.0;
		this.sliderControlMs.MinStep = 1.0;
		this.sliderControlMs.Name = "sliderControlMs";
		this.sliderControlMs.Size = new System.Drawing.Size(302, 28);
		this.sliderControlMs.TabIndex = 7;
		this.sliderControlMs.TipMax = double.NaN;
		this.sliderControlMs.TipMin = double.NaN;
		this.sliderControlMs.Value = 88.88;
		this.sliderControlMs.ValueChanged += new System.EventHandler(sliderControlMs_ValueChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
		base.Controls.Add(this.sliderControlMs);
		base.Controls.Add(this.ledFeed);
		base.Controls.Add(this.txtFeedBack);
		base.Controls.Add(this.lblText);
		base.Margin = new System.Windows.Forms.Padding(4);
		base.Name = "TurnCntl";
		base.Size = new System.Drawing.Size(315, 73);
		((System.ComponentModel.ISupportInitialize)this.ledFeed).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
